package com.biocar.service.impl;

import com.biocar.bean.Balance;
import com.biocar.mapper.BalanceMapper;
import com.biocar.response.BalanceDetail;
import com.biocar.response.PageResponse;
import com.biocar.service.BalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * @author DeSen Xu
 * @date 2021-11-11 15:43
 */
@Service
public class BalanceServiceImpl implements BalanceService {

    private BalanceMapper balanceMapper;

    @Autowired
    public void setBalanceMapper(BalanceMapper balanceMapper) {
        this.balanceMapper = balanceMapper;
    }

    @Override
    public double getNetIncome(Date date) {
        Double balanceByOfTheDay = balanceMapper.getNetIncomeOfTheDay(date);
        return balanceByOfTheDay == null ? 0 : balanceByOfTheDay;
    }


    @Override
    public double getBalanceIn(Date start, Date end) {
        Double balanceIn = balanceMapper.getBalanceIn(start, end);
        return balanceIn == null ? 0 : balanceIn;
    }

    @Override
    public double getBalanceOut(Date start, Date end) {
        Double balanceOut = balanceMapper.getBalanceOut(start, end);
        return balanceOut == null ? 0 : balanceOut;
    }

    @Override
    public PageResponse<BalanceDetail> getBalanceInList(int index, int max, Date start, Date end) {
        Integer total = balanceMapper.getBalanceInCount(start, end);
        if (index > total) {
            return PageResponse.emptyResponse(total, max, index);
        }
        List<BalanceDetail> totalBalanceIn = balanceMapper.getTotalBalanceIn(index, max, start, end);
        return new PageResponse<>(total, max, index, totalBalanceIn);
    }

    @Override
    public PageResponse<BalanceDetail> getBalanceOutList(int index, int max, Date start, Date end) {
        Integer total = balanceMapper.getBalanceOutCount(start, end);
        if (index > total) {
            return PageResponse.emptyResponse(total, max, index);
        }
        List<BalanceDetail> totalBalanceOut = balanceMapper.getTotalBalanceOut(index, max, start, end);
        return new PageResponse<>(total, max, index, totalBalanceOut);
    }

    @Override
    public PageResponse<BalanceDetail> getBalanceOfTheDay(int index, int max, Date start, Date end) {
        Integer totalBalanceCountOfTheDay = balanceMapper.getTotalBalanceCountOfTheDay(start, end);
        if (index > totalBalanceCountOfTheDay) {
            return PageResponse.emptyResponse(totalBalanceCountOfTheDay, max, index);
        }
        List<BalanceDetail> totalBalanceOfTheDay = balanceMapper.getTotalBalanceOfTheDay(index, max, start, end);
        return new PageResponse<>(totalBalanceCountOfTheDay, max, index, totalBalanceOfTheDay);
    }

    @Override
    public void addBalance(Balance balance) {
        balanceMapper.insert(balance);
    }

    @Override
    public void deleteBalance(int id) throws NoSuchElementException {
        int i = balanceMapper.deleteById(id);
        if (i == 0) {
            throw new NoSuchElementException();
        }
    }

    @Override
    public void updateBalance(Balance balance) throws NoSuchElementException, IllegalArgumentException {
        int i;
        try {
            i = balanceMapper.updateById(balance);
        } catch (Exception e) {
            // 项目id插入有误
            throw new IllegalArgumentException(e);
        }
        if (i == 0) {
            throw new NoSuchElementException();
        }
    }

    @Override
    public BalanceDetail getBalanceById(int id) {
        return balanceMapper.getBalanceById(id);
    }

    @Override
    public double getNetIncome(Date start, Date end) {
        Double netIncome = balanceMapper.getNetIncome(start, end);
        return netIncome == null ? 0 : netIncome;
    }
}
